package org.dtrd.modules.evaluation.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.modules.evaluation.entity.bean.PsychologyInfo;
import org.dtrd.modules.evaluation.entity.po.DtrdEntEvaluationRdPsychology;
import org.dtrd.modules.evaluation.mapper.DtrdEntEvaluationRdPsychologyMapper;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdPsychologyService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.StringJoiner;

/**
 * <p>
 * 心理健康临床症状评估 服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-09-26
 */
@Service
@DS("multi-datasource1")
public class DtrdEntEvaluationRdPsychologyServiceImpl extends ServiceImpl<DtrdEntEvaluationRdPsychologyMapper, DtrdEntEvaluationRdPsychology> implements IDtrdEntEvaluationRdPsychologyService {

    @Override
    public PsychologyInfo savePsychologyInfo(PsychologyInfo psychologyInfo) {
        List<Integer> answerArr = psychologyInfo.getInput();
        StringJoiner joiner = new StringJoiner(",");
        int totalScore = 0;
        int tmpScore = 0;
        int positiveNum = 0;
        int ifPositive = 0;

        int f1_somatizationTotal = 0;// 躯体化总分
        int f2_obsessionTotal = 0;// 强迫症总分
        int f3_relationshipTotal = 0;// 人际关系总分
        int f4_depressedTotal = 0;// 抑郁总分
        int f5_anxiousTotal = 0;// 焦虑总分
        int f6_hostileTotal = 0;// 敌对总分
        int f7_errorTotal = 0;// 恐怖总分
        int f8_paranoiaTotal = 0;// 偏执总分
        int f9_mentalTotal = 0;// 精神病总分

        float f1_somatizationScore = 0;// 躯体化
        float f2_obsessionScore = 0;// 强迫症
        float f3_relationshipScore = 0;// 人际关系
        float f4_depressedScore = 0;// 抑郁
        float f5_anxiousScore = 0;// 焦虑
        float f6_hostileScore = 0;// 敌对
        float f7_terrorScore = 0;// 恐怖
        float f8_paranoiaScore = 0;// 偏执
        float f9_mentalScore = 0;// 精神病
        for (int i = 0; i < answerArr.size(); i++) {
            tmpScore = answerArr.get(i);
            joiner.add(tmpScore + "");
            totalScore += tmpScore;
            if (tmpScore >= 1)
                positiveNum++;
            switch (i) {
                case 0:
                case 3:
                case 11:
                case 26:
                case 39:
                case 41:
                case 47:
                case 48:
                case 51:
                case 52:
                case 55:
                case 57:
                    f1_somatizationTotal += tmpScore;
                    break;
                case 2:
                case 8:
                case 9:
                case 27:
                case 37:
                case 44:
                case 45:
                case 50:
                case 54:
                case 64:
                    f2_obsessionTotal += tmpScore;
                    break;
                case 5:
                case 20:
                case 33:
                case 35:
                case 36:
                case 40:
                case 60:
                case 68:
                case 72:
                    f3_relationshipTotal += tmpScore;
                    break;
                case 4:
                case 13:
                case 14:
                case 19:
                case 21:
                case 25:
                case 28:
                case 29:
                case 30:
                case 31:
                case 53:
                case 70:
                case 78:
                    f4_depressedTotal += tmpScore;
                    break;
                case 1:
                case 16:
                case 22:
                case 32:
                case 38:
                case 56:
                case 71:
                case 77:
                case 79:
                case 85:
                    f5_anxiousTotal += tmpScore;
                    break;
                case 10:
                case 23:
                case 62:
                case 66:
                case 73:
                case 80:
                    f6_hostileTotal += tmpScore;
                    break;
                case 12:
                case 24:
                case 46:
                case 49:
                case 69:
                case 74:
                case 81:
                    f7_errorTotal += tmpScore;
                    break;
                case 7:
                case 17:
                case 42:
                case 67:
                case 75:
                case 82:
                    f8_paranoiaTotal += tmpScore;
                    break;
                case 6:
                case 15:
                case 34:
                case 61:
                case 76:
                case 83:
                case 84:
                case 86:
                case 87:
                case 89:
                    f9_mentalTotal += tmpScore;
                    break;
                default:
                    break;
            }
        }
        if (positiveNum > 43)
            ifPositive = 1;
        String totalLevel = "";
        String output = "";

        if (totalScore < 160) {
            totalLevel = "心理健康状况良好";
        } else if (totalScore <= 207) {
            totalLevel = "心理健康处于亚健康状态";
        } else {
            totalLevel = "心理状况不好";
        }
        String f1_somatizationLevel = "";// 躯体化级别
        String f2_obsessionLevel = "";// 强迫症级别
        String f3_relationshipLevel = "";// 人际关系级别
        String f4_depressedLevel = "";// 抑郁级别
        String f5_anxiousLevel = "";// 焦虑级别
        String f6_hostileLevel = "";// 敌对级别
        String f7_terrorLevel = "";// 恐怖级别
        String f8_paranoiaLevel = "";// 偏执级别
        String f9_mentalLevel = "";// 精神病 级别
        f1_somatizationScore = (float) f1_somatizationTotal / 12;
        f2_obsessionScore = (float) f2_obsessionTotal / 10;
        f3_relationshipScore = (float) f3_relationshipTotal / 9;
        f4_depressedScore = (float) f4_depressedTotal / 13;
        f5_anxiousScore = (float) f5_anxiousTotal / 10;
        f6_hostileScore = (float) f6_hostileTotal / 6;
        f7_terrorScore = (float) f7_errorTotal / 7;
        f8_paranoiaScore = (float) f8_paranoiaTotal / 6;
        f9_mentalScore = (float) f9_mentalTotal / 10;
        if (f1_somatizationScore < 2) {
            f1_somatizationLevel = "主观感觉身体健康";
        } else {
            f1_somatizationLevel = "主观感觉身体不适";
        }
        if (f2_obsessionScore < 2) {
            f2_obsessionLevel = "不存在强迫症状";
        } else {
            f2_obsessionLevel = "存在强迫症状";
        }
        if (f3_relationshipScore < 2) {
            f3_relationshipLevel = "不存在人际关系敏感症状";
        } else {
            f3_relationshipLevel = "存在人际关系敏感症状";
        }
        if (f4_depressedScore < 2) {
            f4_depressedLevel = "不存在抑郁症状";
        } else {
            f4_depressedLevel = "存在抑郁症状";
        }
        if (f5_anxiousScore < 2) {
            f5_anxiousLevel = "不存在焦虑症状";
        } else {
            f5_anxiousLevel = "存在焦虑症状";
        }
        if (f6_hostileScore < 2) {
            f6_hostileLevel = "不存在敌对症状";
        } else {
            f6_hostileLevel = "存在敌对症状";
        }
        if (f7_terrorScore < 2) {
            f7_terrorLevel = "不存在恐怖症状";
        } else {
            f7_terrorLevel = "存在恐怖症状";
        }
        if (f8_paranoiaScore < 2) {
            f8_paranoiaLevel = "不存在偏执症状";
        } else {
            f8_paranoiaLevel = "存在偏执症状";
        }
        if (f9_mentalScore < 2) {
            f9_mentalLevel = "不存在精神病性症状";
        } else {
            f9_mentalLevel = "存在精神病性症状";
        }
        DtrdEntEvaluationRdPsychology po = new DtrdEntEvaluationRdPsychology().init();
        po.setInput(joiner.toString());
        po.setTotalScore(totalScore);
        po.setTotalLevel(totalLevel);
        po.setSomatizationScore(f1_somatizationScore);
        po.setSomatizationLevel(f1_somatizationLevel);
        po.setObsessionScore(f2_obsessionScore);
        po.setObsessionLevel(f2_obsessionLevel);
        po.setRelationshipScore(f3_relationshipScore);
        po.setRelationshipLevel(f3_relationshipLevel);
        po.setDepressedScore(f4_depressedScore);
        po.setDepressedLevel(f4_depressedLevel);
        po.setAnxiousScore(f5_anxiousScore);
        po.setAnxiousLevel(f5_anxiousLevel);
        po.setHostileScore(f6_hostileScore);
        po.setHostileLevel(f6_hostileLevel);
        po.setTerrorScore(f7_terrorScore);
        po.setTerrorLevel(f7_terrorLevel);
        po.setParanoiaScore(f8_paranoiaScore);
        po.setParanoiaLevel(f8_paranoiaLevel);
        po.setMentalScore(f9_mentalScore);
        po.setMentalLevel(f9_mentalLevel);
        po.setPositiveNum(positiveNum);
        po.setIfPositive(ifPositive);
        po.setOutput(output);
        if (psychologyInfo.getPatientId() != null) {
            po.setPatientId(psychologyInfo.getPatientId());
            save(po);
        }
        return new PsychologyInfo().parseFromPo(po);
    }

    @Override
    public PsychologyInfo getPsychologyInfoLatest(Integer patientId) {
        LambdaQueryWrapper<DtrdEntEvaluationRdPsychology> queryWrapper = Wrappers.lambdaQuery(DtrdEntEvaluationRdPsychology.class)
                .eq(DtrdEntEvaluationRdPsychology::getPatientId, patientId)
                .orderByDesc(DtrdEntEvaluationRdPsychology::getCreateTime)
                .last("limit 1");
        DtrdEntEvaluationRdPsychology psychology = this.getOne(queryWrapper);
        PsychologyInfo psychologyInfo = new PsychologyInfo();
        if (psychology != null){
            psychologyInfo = psychologyInfo.parseFromPo(psychology);
        }
        return psychologyInfo;
    }
}
